/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.tattoo.workflow.domain.repository;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;
import org.tattoo.workflow.domain.po.ProcurementOrder;

import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
public interface IProcurementOrderRepository extends CrudRepository<ProcurementOrder, Long> {
    /**
     * 修改采购订单
     * 这里只修改流程实例id，订单名称，订单描述，最后更新人，最后更新时间
     *
     * @param procurementOrder 采购订单po对象
     */
    @Query("update ProcurementOrder procurementOrder set procurementOrder.processInstanceId = :#{#procurementOrder.processInstanceId}, procurementOrder.name = :#{#procurementOrder.name}, procurementOrder.description = :#{#procurementOrder.description}, procurementOrder.lastModifiedBy = :#{#procurementOrder.lastModifiedBy}, procurementOrder.lastModifiedDate = :#{#procurementOrder.lastModifiedDate} where procurementOrder.id = :#{#procurementOrder.id}")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    void updateProcurementOrder(ProcurementOrder procurementOrder);

    /**
     * 查询跟传入用户相关的采购订单
     *
     * @param username 用户
     * @return 采购订单
     */
    @Query("select procurementOrder from ProcurementOrder procurementOrder where procurementOrder.creator = :#{#username} or procurementOrder.submitter = :#{#username} or procurementOrder.reviewer = :#{#username} or procurementOrder.approver = :#{#username} order by procurementOrder.lastModifiedDate desc ")
    List<ProcurementOrder> queryProcurementOrders(String username);

    /**
     * 查询状态不为某个值的数据
     *
     * @param status 状态
     * @return 采购订单
     */
    List<ProcurementOrder> queryByStatusNotOrderByLastModifiedDateDesc(Integer status);
}
